package com.doris.media.picker.adapter

import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.doris.media.picker.R
import com.doris.media.picker.model.MediaModel
import com.doris.media.picker.model.MediaPickerConfig
import com.doris.media.picker.model.MediaPickerParameter
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.QMUIRadiusImageView2
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

abstract class BaseMediaPickerAdapter(
        protected val parameter: MediaPickerParameter,
        protected val listener: MediaPickerListener, layout: Int
) : BaseQuickAdapter<MediaModel, BaseViewHolder>(layout), LoadMoreModule {

    protected val mPickerModel = ArrayList<MediaModel>()
    protected var mPlayAudioPosition = -1

    init {
        mPickerModel.addAll(parameter.pickerData)
    }

    fun getPickerSize() = mPickerModel.size

    fun getPickerModel() = mPickerModel

    fun getPlayAudioPosition() = mPlayAudioPosition

    fun updatePlayAudioPosition(position: Int) {
        if (position == mPlayAudioPosition) return
        val oldPosition = mPlayAudioPosition
        mPlayAudioPosition = position
        notifyItemChanged(oldPosition)
        notifyItemChanged(mPlayAudioPosition)
    }
}

class MediaPickerAdapter(parameter: MediaPickerParameter, listener: MediaPickerListener) :
        BaseMediaPickerAdapter(parameter, listener, R.layout.media_picker_item) {
    override fun convert(holder: BaseViewHolder, item: MediaModel) {
        val position = getItemPosition(item)
        val imageView2 = holder.getView<QMUIRadiusImageView2>(R.id.mediaPicker_cover)
        if (item.type == MediaPickerConfig.TYPE_AUDIO) {
            if (parameter.audioIconColorRes != R.color.colorPrimary) {
                imageView2.setColorFilter(
                        ContextCompat.getColor(context, parameter.audioIconColorRes)
                )
            }
        } else {
            Glide.with(context).load(item.path).into(imageView2)
        }
        if (item.type == MediaPickerConfig.TYPE_VIDEO || item.type == MediaPickerConfig.TYPE_AUDIO) {
            holder.setText(R.id.mediaPicker_duration, item.durationTransform)
        } else {
            holder.setText(R.id.mediaPicker_duration, "")
        }

        val checkBox = holder.getView<CheckBox>(R.id.mediaPicker_check)
        val checkBoxMask = holder.getView<View>(R.id.mediaPicker_check_mask)
        val textViewNum = holder.getView<TextView>(R.id.mediaPicker_num)
        val textViewNumOval = holder.getView<TextView>(R.id.mediaPicker_num_oval)
        val imageViewAudio = holder.getView<ImageView>(R.id.mediaPicker_audio_op)

        checkBox.visibility = View.GONE
        checkBoxMask.visibility = View.GONE
        checkBox.isChecked = mPickerModel.contains(item)

        textViewNum.visibility = View.GONE
        textViewNumOval.visibility = View.GONE

        val num = mPickerModel.indexOf(item)
        textViewNum.text = if (num < 0) "" else "${num + 1}"
        textViewNumOval.text = if (num < 0) "" else "${num + 1}"

        val numBg = textViewNum.background as GradientDrawable
        numBg.setStroke(
                QMUIDisplayHelper.dp2px(context, 1),
                ContextCompat.getColor(context, parameter.numBorderColorRes)
        )
        textViewNum.background = numBg

        if (item.type == MediaPickerConfig.TYPE_AUDIO) {
            imageViewAudio.visibility = View.VISIBLE
            if (mPlayAudioPosition == position)
                imageViewAudio.setImageResource(R.drawable.media_picker_pause)
            else
                imageViewAudio.setImageResource(R.drawable.media_picker_play)
        }

        if (parameter.needPreview) {
            imageView2.setOnClickListener { listener.onPreview(imageView2, item) }
            if (parameter.showNum) {
                textViewNumOval.visibility = View.VISIBLE
                textViewNumOval.setOnClickListener {
                    if (mPickerModel.contains(item)) {
                        val index = mPickerModel.indexOf(item)
                        mPickerModel.removeAt(index)
                        for (i in index until mPickerModel.size) {
                            notifyItemChanged(getItemPosition(mPickerModel[i]))
                        }
                        textViewNumOval.text = ""
                    } else {
                        if (mPickerModel.size < parameter.max) {
                            mPickerModel.add(item)
                            textViewNumOval.text = "${mPickerModel.size}"
                        } else {
                            if (parameter.max == 1) {
                                val notifyPosition = getItemPosition(mPickerModel[0])
                                mPickerModel.clear()
                                mPickerModel.add(item)
                                notifyItemChanged(notifyPosition)
                                textViewNumOval.text = "${mPickerModel.size}"
                            } else {
                                listener.showMaxTip()
                            }
                        }
                    }
                }
            } else {
                checkBox.visibility = View.VISIBLE
                checkBoxMask.visibility = View.VISIBLE
                checkBoxMask.setOnClickListener {
                    if (mPickerModel.contains(item)) {
                        val index = mPickerModel.indexOf(item)
                        mPickerModel.removeAt(index)
                        for (i in index until mPickerModel.size) {
                            notifyItemChanged(getItemPosition(mPickerModel[i]))
                        }
                        checkBox.isChecked = false
                    } else {
                        if (mPickerModel.size < parameter.max) {
                            mPickerModel.add(item)
                            checkBox.isChecked = true
                        } else {
                            if (parameter.max == 1) {
                                val notifyPosition = getItemPosition(mPickerModel[0])
                                mPickerModel.clear()
                                mPickerModel.add(item)
                                notifyItemChanged(notifyPosition)
                                checkBox.isChecked = true
                            } else {
                                listener.showMaxTip()
                            }
                        }
                    }
                }
            }
        } else {
            if (parameter.showNum) {
                if (mPickerModel.contains(item)) {
                    textViewNum.visibility = View.VISIBLE
                } else {
                    textViewNum.visibility = View.GONE
                }
            } else {
                checkBox.visibility = View.VISIBLE
            }
            imageView2.setOnClickListener {
                if (mPickerModel.contains(item)) {
                    val index = mPickerModel.indexOf(item)
                    mPickerModel.removeAt(index)
                    for (i in index until mPickerModel.size) {
                        notifyItemChanged(getItemPosition(mPickerModel[i]))
                    }
                    checkBox.isChecked = false
                    textViewNum.visibility = View.GONE
                } else {
                    if (mPickerModel.size < parameter.max) {
                        mPickerModel.add(item)
                        checkBox.isChecked = true
                        textViewNum.text = "${mPickerModel.size}"
                        if (parameter.showNum) textViewNum.visibility = View.VISIBLE
                    } else {
                        if (parameter.max == 1) {
                            val notifyPosition = getItemPosition(mPickerModel[0])
                            mPickerModel.clear()
                            mPickerModel.add(item)
                            notifyItemChanged(notifyPosition)
                            checkBox.isChecked = true
                            textViewNum.text = "${mPickerModel.size}"
                            if (parameter.showNum) textViewNum.visibility = View.VISIBLE
                        } else {
                            listener.showMaxTip()
                        }
                    }
                }
            }
        }
    }
}

class MediaPickerRowAdapter(parameter: MediaPickerParameter, listener: MediaPickerListener) :
        BaseMediaPickerAdapter(parameter, listener, R.layout.media_picker_item_row) {
    private val sdf = SimpleDateFormat("yyyy.MM.dd", Locale.CHINA)

    override fun convert(holder: BaseViewHolder, item: MediaModel) {
        val imageView2 = holder.getView<QMUIRadiusImageView2>(R.id.mediaPicker_cover)
        if (item.type == MediaPickerConfig.TYPE_AUDIO) {
            if (parameter.audioIconColorRes != R.color.colorPrimary) {
                imageView2.setColorFilter(
                        ContextCompat.getColor(context, parameter.audioIconColorRes)
                )
            }
        } else {
            Glide.with(context).load(item.path).into(imageView2)
        }
        holder.setText(R.id.mediaPicker_name, item.name)
        val info = StringBuffer()
        if (item.type == MediaPickerConfig.TYPE_VIDEO || item.type == MediaPickerConfig.TYPE_AUDIO) {
            info.append(item.durationTransform).append("\t\t")
        } else {
            info.append(item.width).append("x").append(item.height).append("\t\t")
        }
        info.append(sdf.format(Date(item.lastModifed))).append("\t\t").append(item.sizeTransform)
        holder.setText(R.id.mediaPicker_info, info.toString())

        val itemView = holder.getView<ConstraintLayout>(R.id.mediaPicker_item)
        val checkBox = holder.getView<CheckBox>(R.id.mediaPicker_check)
        val checkBoxMask = holder.getView<View>(R.id.mediaPicker_check_mask)
        val textViewNumOval = holder.getView<TextView>(R.id.mediaPicker_num_oval)
        val imageViewAudio = holder.getView<ImageView>(R.id.mediaPicker_audio_op)

        if (parameter.isLight) {
            holder.setTextColor(R.id.mediaPicker_name, Color.WHITE)
            checkBox.buttonTintList = ColorStateList.valueOf(Color.WHITE)
            textViewNumOval.setBackgroundResource(R.drawable.media_picker_bg_check_num_oval)
        }

        checkBox.visibility = View.GONE
        checkBoxMask.visibility = View.GONE
        checkBox.isChecked = mPickerModel.contains(item)

        textViewNumOval.visibility = View.GONE

        val num = mPickerModel.indexOf(item)
        textViewNumOval.text = if (num < 0) "" else "${num + 1}"

        if (item.type == MediaPickerConfig.TYPE_AUDIO) {
            imageViewAudio.visibility = View.VISIBLE
            if (mPlayAudioPosition == getItemPosition(item))
                imageViewAudio.setImageResource(R.drawable.media_picker_pause)
            else
                imageViewAudio.setImageResource(R.drawable.media_picker_play)
        }

        if (parameter.needPreview) {
            itemView.setOnClickListener { listener.onPreview(imageView2, item) }
            if (parameter.showNum) {
                textViewNumOval.visibility = View.VISIBLE
                textViewNumOval.setOnClickListener {
                    if (mPickerModel.contains(item)) {
                        val index = mPickerModel.indexOf(item)
                        mPickerModel.removeAt(index)
                        for (i in index until mPickerModel.size) {
                            notifyItemChanged(getItemPosition(mPickerModel[i]))
                        }
                        textViewNumOval.text = ""
                    } else {
                        if (mPickerModel.size < parameter.max) {
                            mPickerModel.add(item)
                            textViewNumOval.text = "${mPickerModel.size}"
                        } else {
                            if (parameter.max == 1) {
                                val notifyPosition = getItemPosition(mPickerModel[0])
                                mPickerModel.clear()
                                mPickerModel.add(item)
                                notifyItemChanged(notifyPosition)
                                textViewNumOval.text = "${mPickerModel.size}"
                            } else {
                                listener.showMaxTip()
                            }
                        }
                    }
                }
            } else {
                checkBox.visibility = View.VISIBLE
                checkBoxMask.visibility = View.VISIBLE
                checkBoxMask.setOnClickListener {
                    if (mPickerModel.contains(item)) {
                        val index = mPickerModel.indexOf(item)
                        mPickerModel.removeAt(index)
                        for (i in index until mPickerModel.size) {
                            notifyItemChanged(getItemPosition(mPickerModel[i]))
                        }
                        checkBox.isChecked = false
                    } else {
                        if (mPickerModel.size < parameter.max) {
                            mPickerModel.add(item)
                            checkBox.isChecked = true
                        } else {
                            if (parameter.max == 1) {
                                val notifyPosition = getItemPosition(mPickerModel[0])
                                mPickerModel.clear()
                                mPickerModel.add(item)
                                notifyItemChanged(notifyPosition)
                                checkBox.isChecked = true
                            } else {
                                listener.showMaxTip()
                            }
                        }
                    }
                }
            }
        } else {
            if (parameter.showNum) {
                textViewNumOval.visibility = View.VISIBLE
            } else {
                checkBox.visibility = View.VISIBLE
            }
            itemView.setOnClickListener {
                if (mPickerModel.contains(item)) {
                    val index = mPickerModel.indexOf(item)
                    mPickerModel.removeAt(index)
                    for (i in index until mPickerModel.size) {
                        notifyItemChanged(getItemPosition(mPickerModel[i]))
                    }
                    checkBox.isChecked = false
                    textViewNumOval.text = ""
                } else {
                    if (mPickerModel.size < parameter.max) {
                        mPickerModel.add(item)
                        checkBox.isChecked = true
                        textViewNumOval.text = "${mPickerModel.size}"
                    } else {
                        if (parameter.max == 1) {
                            val notifyPosition = getItemPosition(mPickerModel[0])
                            mPickerModel.clear()
                            mPickerModel.add(item)
                            notifyItemChanged(notifyPosition)
                            checkBox.isChecked = true
                            textViewNumOval.text = "${mPickerModel.size}"
                        } else {
                            listener.showMaxTip()
                        }
                    }
                }
            }
        }
    }
}

interface MediaPickerListener {
    fun onPreview(view: View, model: MediaModel)

    fun showMaxTip()
}